//
//  PropertyEditors.cpp
//  Editor
//
//  Created by Jeppe Nielsen on 11/14/14.
//  Copyright (c) 2014 Jeppe Nielsen. All rights reserved.
//

#include "PropertyEditors.hpp"
#include <sstream>
#include <limits>

QWidget* PropertyEditorFloat::CreateEditor(SerializedProperty<float>& property, QWidget* parent) {
    return new QDoubleSpinBox(parent);
}

void PropertyEditorFloat::EditingStarted(SerializedProperty<float>& property, QWidget* editor) {
    QDoubleSpinBox* spinner = static_cast<QDoubleSpinBox*>(editor);
    spinner->setMaximum(std::numeric_limits<double>::max());
    spinner->setMinimum(std::numeric_limits<double>::min());
    spinner->setValue(property.GetValue());
}

void PropertyEditorFloat::EditingFinished(SerializedProperty<float>& property, QWidget* editor) {
    QDoubleSpinBox* spinner = static_cast<QDoubleSpinBox*>(editor);
    property.SetValue((float)spinner->value());
}

void PropertyEditorFloat::GetValue(SerializedProperty<float>& property, QVariant* value) {
    std::stringstream s;
    s<<property.GetValue();
    value->setValue(QString(s.str().c_str()));
}

QWidget* PropertyEditorInt::CreateEditor(SerializedProperty<int32_t>& property, QWidget* parent) {
    return new QSpinBox(parent);
}

void PropertyEditorInt::EditingStarted(SerializedProperty<int32_t>& property, QWidget* editor) {
    QSpinBox* spinner = static_cast<QSpinBox*>(editor);
    spinner->setMaximum(std::numeric_limits<int32_t>::max());
    spinner->setMinimum(std::numeric_limits<int32_t>::min());
    spinner->setValue(property.GetValue());
}

void PropertyEditorInt::EditingFinished(SerializedProperty<int32_t>& property, QWidget* editor) {
    QSpinBox* spinner = static_cast<QSpinBox*>(editor);
    property.SetValue((float)spinner->value());
}

void PropertyEditorInt::GetValue(SerializedProperty<int32_t>& property, QVariant* value) {
    std::stringstream s;
    s<<property.GetValue();
    value->setValue(QString(s.str().c_str()));
}


QWidget* PropertyEditorString::CreateEditor(SerializedProperty<std::string>& property, QWidget* parent) {
    return new QLineEdit(parent);
}

void PropertyEditorString::EditingStarted(SerializedProperty<std::string>& property, QWidget* editor) {
    QLineEdit* textEditor = static_cast<QLineEdit*>(editor);
    textEditor->setText(QString(property.GetValue().c_str()));
}

void PropertyEditorString::EditingFinished(SerializedProperty<std::string>& property, QWidget* editor) {
    QLineEdit* textEditor = static_cast<QLineEdit*>(editor);
    property.SetValue(textEditor->text().toStdString());
}

void PropertyEditorString::GetValue(SerializedProperty<std::string>& property, QVariant* value) {
    value->setValue(QString(property.GetValue().c_str()));
}
